//
//  LSExtensionQueryViewController.m
//  lightSpot
//
//  Created by lightSpot on 2023/12/31.
//  Copyright © 2023 lightSpot. All rights reserved.
//

#import "LSExtensionQueryViewController.h"

#import "LSExtensionQueryTextCell.h"
#import "LSExtensionQueryButtonCell.h"

#import "LSQueryResultsViewController.h"

@interface LSExtensionQueryViewController ()

@property (nonatomic,copy) NSString *word;

@end

@implementation LSExtensionQueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationBar.backgroundColor = [UIColor clearColor];
    [self setNavigationItemTitleViewWithTitle:@"拓展名查询"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LSExtensionQueryTextCell" bundle:nil] forCellReuseIdentifier:@"LSExtensionQueryTextCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"LSExtensionQueryButtonCell" bundle:nil] forCellReuseIdentifier:@"LSExtensionQueryButtonCell"];
    
    self.tableView.frame = CGRectMake(0,self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight);
}

#pragma mark - cellDataSource

- (NSArray *)cellDataSource {
    
    if (!self.dataSource) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
        
        __block NSMutableArray *subarr = nil;
        __block NSDictionary *dict = nil;
                
        subarr = [NSMutableArray arrayWithCapacity:1];
        
        dict =  @{@"class":LSExtensionQueryTextCell.class,
                  @"height":@([LSExtensionQueryTextCell getCellFrame:[NSNumber numberWithFloat:500]]),
                  @"data":[LSExtensionQueryTextCellModel ittemModelWithTitle:@"" content:_word tip:@"请输入需要查询的拓展名" max:@"10"],
                  @"delegate":@YES};
        [subarr addObject:dict];
        
        dict =  @{@"class":LSExtensionQueryButtonCell.class,
                  @"height":@([LSExtensionQueryButtonCell getCellFrame:[NSNumber numberWithFloat:190]]),
                  @"data":[LSExtensionQueryButtonCellModel ittemModelWithTitle:@"查询"],
                  @"delegate":@YES};
        [subarr addObject:dict];
                
        [arr addObject:subarr];

        self.dataSource = arr;
    }
    return self.dataSource;
}

#pragma mark LSExtensionQueryTextCellDelegate 拓展名输入

- (void)extensionQueryTextCell:(LSExtensionQueryTextCell *)cell textView:(UITextView *)textView {
    _word = textView.text;
}

#pragma mark ECextensionQueryButtonCellDelegate 保存

- (void)extensionQueryButtonCell:(LSExtensionQueryButtonCell *)cell button:(UIButton *)sender {
    NSString *erro = @"";

    if (_word.length == 0) {
        erro = @"请输入需要查询的拓展名";
    }
    
    if (erro.length > 0) {
        [self showProgressHUD:erro];
    } else {
        [self gotoQueryResultsViewController];
    }
}

#pragma mark - goto

- (void)gotoQueryResultsViewController {
    LSQueryResultsViewController* vc = [LSQueryResultsViewController new];
    vc.word = _word;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
